<template>
  <div class="aui-wrapper aui-page__login">
    <div class="aui-content__wrapper">
      <main class="aui-content">
        <div class="login-header">
          <h2 class="login-brand">党建“驿站”</h2>
        </div>
        <div class="login-body">
          <h3 class="login-title">登录</h3>
          <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" status-icon>
            <el-form-item prop="username">
              <el-input clearable v-model="dataForm.username" @input="handleUser" placeholder="用户名">
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input clearable v-model="dataForm.password" @input="handlePass" type="password"  show-password placeholder="密码">
              </el-input>
            </el-form-item>
            <!-- 验证码 -->
            <el-form-item prop="captcha">
              <!-- <el-row :gutter="20">
                <el-col :span="14">
                  <el-input @keyup.enter.native="handleLogin" v-model="dataForm.captcha" placeholder="验证码">
                    <span slot="prefix" class="el-input__icon">
                      <svg class="icon-svg" aria-hidden="true"><use xlink:href="#icon-safetycertificate"></use></svg>
                    </span>
                  </el-input>
                </el-col> -->
                <!-- 验证码图片部分 -->
                <!-- <el-col :span="10" class="login-captcha">
                  <img :src="captchaPath" @click="getCaptcha()">
                </el-col>
              </el-row> -->
            </el-form-item>
            <!--  -->
            <el-form-item>
              <el-button type="primary" @click="dataFormSubmitHandle()" class="w-percent-100">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </main>
    </div>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import debounce from 'lodash/debounce'
// import axios from 'axios';
// import { getUUID } from '@/utils'
export default {
  inject: ['reload'],
  data () {
    return {
      captchaPath: '',
      dataForm: {
        username: '',
        password: '',
        // uuid: '',
        // captcha: ''
      },
      inputPass:"",
    }
  },
  computed: {
    dataRule () {
      return {
        username: [
          { required: true, message: "用户名不能为空", trigger: 'blur' }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: 'blur' }
        ],
        // captcha: [
        //   { required: true, message: "验证码不能为空", trigger: 'blur' }
        // ]
      }
    }
  },
  created () {
    // this.getCaptcha()
      // this.handleLogin()
  },
  methods: {
    // 获取验证码
    // getCaptcha () {
    //   this.dataForm.uuid = getUUID()
    //   this.captchaPath = `${window.SITE_CONFIG['apiURL']}/captcha?uuid=${this.dataForm.uuid}`
    // },
    handleUser(e){///监听用户名输入框
      // console.log(e)
    },
    handlePass(e){//监听密码输入框
      // console.log(e)
    },
    // 登录按钮
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        // console.log(valid)
        if (!valid) {
          return false
        }
        this.axios({
            method: 'post',
            // url: "/api/user/login",
            url: "/user/login",
            data: this.dataForm,
          }).then(res=>{
              // console.log(res)
              if(res.data.code==20001){
                var userinfo = {
                  username:this.dataForm.username,//用户的用户名
                  password:this.dataForm.password,//密码
                  token:res.data.data.authorizationToken,//用户token
                  authority:[],//权限等级
                }
                for(var i=0;i<res.data.data.levelRole.length;i++){
                  userinfo.authority.push(res.data.data.levelRole[i])
                }
                window.SITE_CONFIG['userinfo'] = res.data.data
                // console.log(userinfo)
                // // 将token的值存储到本地
                localStorage.setItem('token', res.data.data.authorizationToken);
                // localStorage.setItem('data',JSON.stringify(res.data.data))
                // 将登录的用户信息存储到本地
                localStorage.setItem('userinfo',JSON.stringify(userinfo))
                // console.log(userinfo)
                this.$router.replace({ name: 'home',query:res.data.data })
                // 进入页面后刷新一次
                // window.location.reload()
                // this.$router.push(`${this.$route.path}?t=${Date.now()}`)
              }
              else{
                // 提示错误信息
                return this.$message.error(res.data.msg)
              }
              // console.log(res)
              Cookies.set('token', res.data.data.authorizationToken)
              // this.$router.replace({ name: 'home' })
            }).catch(() => {})
          })
    }, 1000, { 'leading': true, 'trailing': false }),
  }
}
</script>
